package leetcode;

/**
 * @Description
 * @Author away
 * @Version V1.0.0
 * @Since 1.0
 * @Date 21.5.18
 */

public class T1442 {
    public static void main(String[] args) {
        int[] arr = {2, 3};
//        int[] arr = {1, 1, 1, 1, 1};
//        int[] arr = {2, 3, 1, 6, 7};
        T1442 t1442 = new T1442();
        System.out.println(t1442.countTriplets(arr));
        System.out.println("--------");
    }

    public int countTriplets(int[] arr) {
        int count = 0;
        int[] s = new int[arr.length+1];
        s[0] = arr[0];
        for (int i = 0; i < arr.length; ++i) {
            s[i + 1] = s[i] ^ arr[i];
        }
        for (int i = 0; i < arr.length; ++i) {
            for (int k = i + 1; k < arr.length; ++k) {
                if (s[i] == s[k + 1]) {
                    count += k - i;
                }
            }
        }


        return count;
    }


    /**
     * 暴力解法，不行
     *
     * @param arr
     * @return
     */
    public int countTriplets1(int[] arr) {
        int count = 0;
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = i + 1; j < arr.length; j++) {
                for (int k = j; k < arr.length; k++) {
                    int a = countXOR(arr, i, j);
                    int b = countXOR(arr, j, k + 1);
                    if (a == b) {
                        count++;
                    }
                }
            }
        }
        return count;
    }

    public int countXOR(int[] arr, int start, int end) {
        int result = 0;
        for (int i = start; i < end; i++) {
            result ^= arr[i];
        }
        return result;
    }
}
